package com.backend.modules.system.entity;

import com.backend.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
@Table(name = "sys_department")
@EqualsAndHashCode(callSuper = true)
@ToString(exclude = {"parent", "children", "users"})
@SQLDelete(sql = "UPDATE sys_department SET deleted = true WHERE id = ?")
@Where(clause = "deleted = false")
public class SysDepartment extends BaseEntity {

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private Integer type;

    @Column(name = "parent_id", insertable = false, updatable = false)
    private Long parentId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    @JsonIgnore
    private SysDepartment parent;

    @OneToMany(mappedBy = "parent")
    @JsonIgnore
    private List<SysDepartment> children = new ArrayList<>();

    @OneToMany(mappedBy = "department")
    @JsonIgnore
    private List<SysUser> students = new ArrayList<>();

    private Integer sort;
} 